package com.xujianlun.cpms.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujianlun.cpms.exception.ServiceException;
import com.xujianlun.cpms.mapper.RecordMapper;
import com.xujianlun.cpms.utils.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.common.Response;

import com.xujianlun.cpms.service.IRecordService;
import com.xujianlun.cpms.entity.Record;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-12-21
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    @Resource
    private IRecordService recordService;
    @Resource
    private RecordMapper recordMapper;
    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody Record record) {
        //获取当前用户的userid
        Integer userId = TokenUtil.getTokenUserId();
        Boolean check = recordService.check(userId);
        if(check==true){
            throw new ServiceException(-200,"今日已经提交");
        }
        record.setUserId(userId);
        record.setAddTime(DateUtil.now());
        recordService.save(record);
        return Response.success();
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        recordService.removeById(id);
        return Response.success();
    }

    @PostMapping("/del/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        recordService.removeByIds(ids);
        return Response.success();
    }

    @GetMapping
    public Response findAll() {
        return Response.success(recordService.list());
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(recordService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Response.success(recordService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/pageByLocale")
    public Response findPageByLocale(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     @RequestParam(defaultValue = "") String name,
                                     @RequestParam(defaultValue = "") Integer userId,
                                     @RequestParam(defaultValue = "") String locale,
                                     @RequestParam(defaultValue = "") String localName){
        return Response.success(recordMapper.findPageByLocale(new Page<>(pageNum, pageSize),name,locale,userId,localName));
    }

    @GetMapping("/pageByUserId/{userId}")
    public Response findPageByUserId(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     @PathVariable Integer userId){
        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();
        recordQueryWrapper.eq("user_id",userId);
        return Response.success(recordService.page(new Page<>(pageNum, pageSize),recordQueryWrapper));
    }
}

